
import { _decorator, Component, Node, game, Prefab, instantiate } from 'cc';
import { ItemList } from './item_list';
const { ccclass, property } = _decorator;

@ccclass('SceneList')
export class SceneList extends Component {

    @property ({ type: Prefab })
    public itemPrefab: Prefab | null  = null;

    public sceneArray:string[]=[]

    __preload(){
        //@ts-ignore
        const sceneInfo = game._sceneInfos;
        let firstIndex = 0;
        let lastIndex = 0;
        let sceneString: string = '';
        for (let i = 0; i < sceneInfo.length; i++){
            sceneString = sceneInfo[i].url;
            firstIndex = sceneString.lastIndexOf('/') + 1;
            lastIndex = sceneString.lastIndexOf('.scene');
            sceneString = sceneString.substring(firstIndex, lastIndex);
            if (sceneString === 'scene_list') {
                continue;
            }
            this.sceneArray.push(sceneString);
        }
    }

    start () {
        this.updateList();
    }

    // update (deltaTime: number) {
    //     // [4]
    // }

    updateList(){
        if(!this.itemPrefab){
            return
        }
        this.node.removeAllChildren();
        for (let i = 0; i < this.sceneArray.length; i++ ) {
            const item = instantiate(this.itemPrefab);
            this.node.addChild(item);
            
            let itemList=item.getComponent(ItemList);
            itemList.updateItem(this.sceneArray[i]);
        }
    }
}